***  Merges child panel with FE, prices and Census predicted wages
set more off


log using "$log_files/2_create_child_data", replace


*******************************************************************
*************   A. import & save PSID data for children   *********
*******************************************************************
*** Read panel data for all children in CDS (observations by kid-year)
clear
use "$data/ChildPanelCDS.dta"
rename *, lower

*******************************************************************
*************  B. Initial variable construction   *****************
*******************************************************************


*drop PSID state, keep FIPS 
drop state
rename stfips  state                         // rename state fips code 'state' for merging
rename childcare_exp chcare_ann           // rename annual childcare expenditures from main PSID survey
rename m_pc_st m_pc97							 // rename "mom" passage comprehension score from 1997

* Generate educational categories
gen med_cat = .
replace med_cat=1 if med_hsd==1
replace med_cat=2 if med_hs==1
replace med_cat=3 if med_scoll==1
replace med_cat=4 if med_collplus==1

gen fed_cat = .
replace fed_cat=1 if fed_hsd==1
replace fed_cat=2 if fed_hs==1
replace fed_cat=3 if fed_scoll==1
replace fed_cat=4 if fed_collplus==1

duplicates report kid year



* Generate some age-related variables

gen num_13plus = num_child - num_0_12

gen old_child = (num_13plus>=1)
replace old_child=. if num_13plus==.

gen old_child_2plus = (num_13plus>=2)
replace old_child_2plus=. if num_13plus==.


gen age9_12 = (age>=9 & age<=12)
replace age9_12=. if age==.
	

*Impute grandparents or other relatives in HH using the lag for non-interview years after 1997

xtset kid year
replace biol_grand_hh=L1.biol_grand_hh if year==1998|year==2000|year==2002|year==2004|year==2006
replace older_relative_hh=L1.older_relative_hh if year==1998|year==2000|year==2002|year==2004|year==2006




*Save full child's panel
save "$temp/parchild1.dta", replace 



**********************************************************************************
* C. Merge predicted wages from Census using occupation, education, race, state  *
***********************************************************************************

*** Prepare predicted wages for merge for mothers and fathers

* Use minor occupation categorization



use "$data/predicted_lnwage3", clear
*keep predicted wages for mothers
keep if  female==1
*Rename variables to m_*
rename occ_minor m_occ_minor
rename predicted_*  m_pred_*
rename race m_white
rename n_obs m_n_obs
sort m_occ_minor state educ m_white
save "$temp/mother_occupation.dta", replace 


use "$data/predicted_lnwage3", clear
*keep predicted wages for fathers
keep if  female==0
*Rename variables to f_*
rename occ_minor f_occ_minor
rename predicted_* f_pred_*
rename race f_white
rename n_obs f_n_obs
sort f_occ_minor state educ f_white
save "$temp/father_occupation.dta", replace 



*Merge predicted wages with the panel of children
use "$temp/parchild1.dta", clear

*Occupation is not reported in 2002: use occupation in 2003 if available 
//- if 2003 occupation missing - use 2001 occupation
xtset kid year
replace m_occ_minor=F1.m_occ_minor if year==2002
replace m_occ_minor=L1.m_occ_minor if year==2002 & m_occ_minor==.

replace f_occ_minor=F1.f_occ_minor if year==2002
replace f_occ_minor=L1.f_occ_minor if year==2002 & f_occ_minor==.

*Generate consistent education category for merge with mother's predicted wages
generate educ=.
replace educ=0 if m_ed=="<12"
replace educ=1  if m_ed=="12"
replace educ=2  if m_ed=="13-15"
replace educ=3  if m_ed=="16"
replace educ=4  if m_ed==">16"

sort m_occ_minor state educ m_white
merge m:1 m_occ_minor state educ m_white using "$temp/mother_occupation.dta", keep(match master)
tab _merge

drop _merge educ


*Generate consistent education category for merge with father's predicted wages 
generate educ=.
replace educ=0 if f_ed=="<12"
replace educ=1  if f_ed=="12"
replace educ=2  if f_ed=="13-15"
replace educ=3  if f_ed=="16"
replace educ=4  if f_ed==">16"

sort f_occ_minor state educ f_white
merge m:1 f_occ_minor state educ f_white using "$temp/father_occupation.dta", keep(match master)
tab _merge

******************************************************************

drop _merge educ 

sort state year 
save "$temp/parchild2.dta", replace


*******************************************************************
******** D.  Merge with state-year price data ***********
*******************************************************************

clear
use "$data/p_extendedcosts.dta"

drop state_year

rename stateid state
rename all p_all
rename goods p_goods
rename servo p_serv



sort state year

merge 1:m state year using "$temp/parchild2.dta", keep(match using)
tab year _merge
drop _merge

sort state 

*Deflate prices to 2002 dollars


local p1 " p_goods p_all p_serv"


foreach var of varlist `p1' p_icent_* p_ifcc_* p_yocent_* p_yofcc_* {
generate `var'_d=`var'/cpiu
replace `var'=`var'_d
drop `var'_d
}


save "$temp/parchild3.dta", replace  // Save data for additional merging




*******************************************************************
****************** E.  merge in fixed effects *********************
*******************************************************************

sort mid 

merge m:1 mid using "$temp/ln_HCm_dat", keep(match master)
tab _merge
drop _merge

merge m:1 fid using "$temp/ln_HCf_dat", keep(match master)
tab _merge
drop _merge



*********************************************
*** F.  SAVE THE FULL PARENT-CHILD DATA ****
*********************************************
sort kid year

save "$temp/psid_child.dta", replace


**** Erase temporary datasets **** 

erase "$temp/parchild1.dta"
erase "$temp/parchild2.dta"
erase "$temp/parchild3.dta"
erase "$temp/mother_occupation.dta"
erase "$temp/father_occupation.dta"

log close


